﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class FightPanel : MonoBehaviour
{

    public GameObject m_Group1;
    public GameObject m_Group2;
    public GameObject m_Group3;
    public GameObject m_Group4;
    public GameObject m_MotherShipUI;
    public Control m_Controle;

    public Image m_StructLine;
    public Image m_ElectricLine;
    List<GameObject> m_Groups = new List<GameObject>();

    public GameObject m_WeaponSlotIconPerfab;

    public Ship Ship
    {
        set {
            m_Ship = value;
            if (!m_Ship)
                return;
            Clear();
            foreach (var slot in m_Ship.m_WeaponSlots)
            {
                var obj = Instantiate(m_WeaponSlotIconPerfab);
                WeaponSlotIcon slotIcon = obj.GetComponent<WeaponSlotIcon>();
                slotIcon.Slot = slot;
                int groupIndex = slot.WeaponGroup - 1;
                m_Groups[groupIndex].SetActive(true);
                if (groupIndex < 4)
                {
                    obj.transform.SetParent(m_Groups[groupIndex].transform);
                    obj.transform.localScale = new Vector3(1,1,1);
                }
            }

            if(m_Ship as MotherShip != null)
                m_MotherShipUI.SetActive(true);
            else
                m_MotherShipUI.SetActive(false);
        }

        get { return m_Ship; }
    }
    Ship m_Ship;
    void Awake()
    {
        m_Groups.Add(m_Group1);
        m_Groups.Add(m_Group2);
        m_Groups.Add(m_Group3);
        m_Groups.Add(m_Group4);
        Clear();

        m_Controle.OnControleShipChanged.AddListener(OnControleShipChanged);

    }
    void OnControleShipChanged(Ship ship)
    {
        Ship = ship;
    }

    void Update()
    {
        float structPointScale = m_Ship.m_ShipStruct.StructurePoint / m_Ship.m_ShipStruct.StructurePointMax;
        float electricScale = m_Ship.m_ShipStruct.Electric / m_Ship.m_ShipStruct.m_ElectricMax;
        m_StructLine.transform.localScale = new Vector3(structPointScale, 1, 1);
        m_ElectricLine.transform.localScale = new Vector3(electricScale, 1, 1);
    }

    public void OnPullHook()
    {

    }

    void Clear()
    {
        for (int i = 0; i < m_Groups.Count; ++i)
        {
            for (int j = 0; j < m_Groups[i].transform.childCount; ++j)
            {
                GameObject.Destroy(m_Groups[i].transform.GetChild(j).gameObject);
            }
        }
        m_Group1.SetActive(false);
        m_Group2.SetActive(false);
        m_Group3.SetActive(false);
        m_Group4.SetActive(false);
    }
}
